package org.rcy.framework.data.mybatis;

import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.rcy.framework.data.datasource.Database;
import org.rcy.framework.data.datasource.SimulateDataSource;

import javax.sql.DataSource;
import java.util.Properties;

public class DatabaseIdProviderImpl extends VendorDatabaseIdProvider {

	public DatabaseIdProviderImpl() {
		super();
		Properties p = new Properties();
		for (Database db : Database.values()) {
			p.setProperty(db.productName(), db.databaseId());
		}
		setProperties(p);
	}

	@Override
	public String getDatabaseId(DataSource dataSource) {
		if (dataSource instanceof SimulateDataSource) {
			SimulateDataSource simulate = (SimulateDataSource) dataSource;
			if (simulate.getDelegate() == null) {
				return Database.MYSQL.databaseId();
			}
		}
		return super.getDatabaseId(dataSource);
	}

}
